package com.liu.xml;

import com.liu.common.ParentTemplate;
import com.liu.common.Template;
import com.liu.entity.Property;

import java.util.List;

/**
 * @author Mr.Liu
 * @version 1.0
 * @date 2024/8/11 11:17
 */
public class XmlTemplate extends ParentTemplate implements Template {

    private static final String HEAD_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
            "<!DOCTYPE mapper\n" +
            "PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n" +
            "\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">";
    private static final String MAPPER  = "Mapper";
    private static  final String MAP = "Map";





    @Override
    public String toTemplate() {
        StringBuffer sb = new StringBuffer(HEAD_TEMPLATE);

        //resultMap start
        sb.append("\n\n<mapper namespace=\""+this.getPackageName()+".dao."+this.getMapperName()+"\">\n");
        sb.append("    <resultMap id="+"\""+this.getClassName()+MAP+"\" type=\""+this.getPackageName()+this.getModelName()+"\">\n");

        List<Property> varList  = this.getVarList();
        for (Property property : varList) {
            sb.append("        <result  column=\""+property.getColumnsName()+"\""+"    property=\""+property.getVarName()+"\"/>\n");
        }

        sb.append("        <result  column=\"creat_by\"  property=\"createdBy\"/>\n");
        sb.append("        <result  column=\"creat_dte\" property=\"createdDate\"/>\n");
        sb.append("        <result  column=\"last_updte_by\"  property=\"lastUpdatedBy\"/>\n");
        sb.append("        <result  column=\"last_updte_dte\"  property=\"lastUpdatedDate\"/>\n");

        sb.append("    </resultMap>\n\n");
        //result end


        //insert start
        sb.append("    <insert id=\"insert\" parameterType=\""+this.getPackageName()+ ".model."+this.getModelName()+ ">\n");
        sb.append("        INSERT INTO "+this.getTableName()+"(\n");

        for(int ii = 0; ii < varList.size() ;ii++){
            //插入时不插入id
            if(varList.get(ii).getColumnsName().equals(this.getColumnPrimaryKey())){
                continue;
            }

            if(ii == 0){
                sb.append("            "+varList.get(ii).getColumnsName()+"\n");
            }else{
                sb.append("            ,"+varList.get(ii).getColumnsName()+"\n");
            }
        }
        sb.append("            ,creat_by\n");
        sb.append("            ,creat_dte\n");
        sb.append("            ,last_updte_by\n");
        sb.append("            ,last_updte_dte\n");
        sb.append("        ) VALUES (\n");

        for(int ii = 0; ii < varList.size() ;ii++){
            //插入时不插入id
            if(varList.get(ii).getColumnsName().equals(this.getColumnPrimaryKey())){
                continue;
            }
            if(ii == 0){
                sb.append("            #{"+varList.get(ii).getVarName()+"}\n");
            }else{
                sb.append("            ,#{"+varList.get(ii).getVarName()+"}\n");
            }
        }
        sb.append("            ,#{createdBy}\n");
        sb.append("            ,NOW()\n");
        sb.append("            ,#{lastUpdatedBy}\n");
        sb.append("            ,NOW()\n");
        sb.append("        )\n");
        sb.append("    </insert>\n\n");

        //insert end

        //delete start
        sb.append("    <delete id=\"delete\" parameterType=\""+this.getPackageName()+".model."+this.getModelName()+"\" resultType=\"java.lang.Integer\" >\n");
        sb.append("        "+"DELETE FROM "+this.getTableName() +" WHERE "+this.getColumnPrimaryKey()+"=#{"+this.getPrimaryKey()+"}\n");
        sb.append("    </delete>\n\n");
        //delete end


        //update start
        sb.append("    <update id=\"update\" parameterType=\""+this.getPackageName()+".model."+this.getModelName()+"\" resultType=\"java.lang.Integer\" >\n");
        sb.append("        UPDATE "+this.getTableName() +" SET\n");
        for(int ii = 0 ; ii < varList.size() ; ii++){
            if(ii == 0 ){
                sb.append("            "+varList.get(ii).getColumnsName() + " = #{"+varList.get(ii).getVarName()+"}\n");
            }else{
                sb.append("            ,"+varList.get(ii).getColumnsName() + " = #{"+varList.get(ii).getVarName()+"}\n");
            }
        }
        sb.append("            ,last_updte_by = #{lastUpdatedBy}\n");
        sb.append("            ,last_updte_dte = NOW()\n");

        sb.append("        WHERE "+this.getColumnPrimaryKey()+" = #{"+this.getPrimaryKey()+"}\n");
        sb.append("    </update>\n\n");
        //update end


        //getLastUpdate start
        sb.append("    <select id=\"getLastUpdateDate\" resultType=\"java.util.Date\" parameterType=\"java.lang.Integer\">\n" +
                "        SELECT last_updte_dte FROM "+this.getTableName()+" WHERE "+this.getColumnPrimaryKey()+" =#{"+this.getPrimaryKey()+"}\n" +
                "    </select>\n\n");
        //getLastUpdate end


        //searchById start
        sb.append("    <select id=\"searchById\" resultMap=\""+this.getClassName()+MAP+"\""+ "parameterType=\"java.lang.Integer\">\n");
        sb.append("        SELECT\n");
        for(int ii = 0 ; ii < varList.size() ; ii++){
            if(ii == 0 ){
                sb.append("            "+varList.get(ii).getColumnsName()+"\n");
            }else{
                sb.append("            ,"+varList.get(ii).getColumnsName() +"\n");
            }
        }
        sb.append("            ,creat_by\n");
        sb.append("            ,creat_dte\n");
        sb.append("            ,last_updte_by\n");
        sb.append("            ,last_updte_dte\n");
        sb.append("        FROM "+this.getTableName()+"\n");
        sb.append("    </select>\n\n");
        //searchById end

        //search start
        sb.append("    <select id=\"search\" resultMap=\""+this.getClassName()+MAP+"\""+ " parameterType=\""+this.getPackageName()+".model."+this.getModelName()+"\""+">\n");
        sb.append("        SELECT\n");
        for(int ii = 0 ; ii < varList.size() ; ii++){
            if(ii == 0 ){
                sb.append("            "+varList.get(ii).getColumnsName()+"\n");
            }else{
                sb.append("            ,"+varList.get(ii).getColumnsName() +"\n");
            }
        }

        sb.append("            ,creat_by\n");
        sb.append("            ,creat_dte\n");
        sb.append("            ,last_updte_by\n");
        sb.append("            ,last_updte_dte\n");
        sb.append("        FROM "+this.getTableName()+"\n");
        sb.append("    </select>\n\n");
        //search end

        //searchCount start
        sb.append("    <select id=\"searchCount\" resultType=\"java.lang.Integer\""+" parameterType=\""+this.getPackageName()+".model."+this.getModelName()+"\" > \n");
        sb.append("        SELECT \n");
        for(int ii = 0 ; ii < varList.size() ; ii++){
            if(ii == 0 ){
                sb.append("            "+varList.get(ii).getColumnsName()+"\n");
            }else{
                sb.append("            ,"+varList.get(ii).getColumnsName() +"\n");
            }
        }
        sb.append("        FROM "+this.getTableName()+"\n");
        sb.append("    </select>\n\n");
        //searchCount end


        //deleteByIds start
        sb.append("    <delete id=\"deleteByIds\"  parameterType=\"java.util.List\">\n");
        sb.append("        DELETE FROM "+ this.getTableName()+"\n");
        sb.append("        WHERE "+this.getColumnPrimaryKey()+" IN (\n");
        sb.append("            <foreach collection=\"list\" item=\"item\" separator=\",\" >\n");
        sb.append("                #{item}\n");
        sb.append("            </foreach>\n");
        sb.append("        )\n");
        sb.append("    </delete>\n\n");
        //deleteByIds end

        //deleteNoInIds start
        sb.append("    <delete id=\"deleteNoInIds\"  parameterType=\"java.util.List\">\n");
        sb.append("        DELETE FROM "+ this.getTableName()+"\n");
        sb.append("        WHERE "+this.getColumnPrimaryKey()+" NOT IN (\n");
        sb.append("            <foreach collection=\"list\" item=\"item\" separator=\",\" >\n");
        sb.append("                #{item}\n");
        sb.append("            </foreach>\n");
        sb.append("        )\n");
        sb.append("    </delete>\n\n");
        //delete end


        sb.append("</mapper>");
        return sb.toString();
    }
}
